//
//  UIColor+Hex.m
//  YanXiuWang
//
//  Created by 招利 李 on 16/11/22.
//  Copyright © 2016年 小分队. All rights reserved.
//

#import "UIColor+Hex.h"

@implementation UIColor (Hex)

- (NSString *)HEXString {
    CGFloat r = 0;
    CGFloat g = 0;
    CGFloat b = 0;
    CGFloat a = 1;
    
    [self getRed:&r green:&g blue:&b alpha:&a];
    
    uint32_t aa = (uint32_t)(255 * a) << 24;
    uint32_t rr = (uint32_t)(255 * r) << 16;
    uint32_t gg = (uint32_t)(255 * g) <<  8;
    uint32_t bb = (uint32_t)(255 * b) <<  0;
    
    // 得到 ARGB 格式的的颜色值 并已16进制格式输出
    return [NSString stringWithFormat:@"%X", aa | rr | gg | bb];
}


+ (UIColor *)colorWithString:(NSString *)value {
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:@"(?:#?|0x)([0-9a-fA-F]{2,8})" options:NSRegularExpressionCaseInsensitive error:nil];
    
    NSTextCheckingResult *result = [regular firstMatchInString:value options:0 range:NSMakeRange(0, value.length)];
    
    if (result == nil) {
        printf("\nError:-->>无法解析颜色:%s\n", [value cStringUsingEncoding:NSUTF8StringEncoding]);
        return [UIColor lightGrayColor];
    }
    
    // 只有2位认为是灰阶 6位认为是 不透明颜色 8位认为是自定义透明度颜色
    NSString *number = [value substringWithRange:[result rangeAtIndex:1]];
    switch (number.length) {
        case 2:
            number = [NSString stringWithFormat:@"FF%@%@%@",number,number,number];
            break;
        case 6:
            number = [NSString stringWithFormat:@"FF%@",number];
            break;
        case 8:
            break;
        default:
            printf("\nError:-->>无法解析颜色:%s\n", [value cStringUsingEncoding:NSUTF8StringEncoding]);
            return [UIColor lightGrayColor];;
    }
    //print(@"颜色值:->%@",number);
    // 字符串转数值 并用数值初始化颜色
    uint32_t color;
    NSScanner *scanner = [NSScanner scannerWithString:number.lowercaseString];
    [scanner scanHexInt:&color];
    
    return [UIColor colorWithValue:color];
}

+ (UIColor *)colorWithValue:(uint32_t)value {
    
    uint32_t a = value > 0xFFFFFF ? value >> 24 : 0xFF;
    uint32_t r = (value >> 16) & 0xFF;
    uint32_t g = (value >>  8) & 0xFF;
    uint32_t b = (value >>  0) & 0xFF;
    
    return [UIColor colorWithRed:(CGFloat)r/255.0 green:(CGFloat)g/255.0 blue:(CGFloat)b/255.0 alpha:(CGFloat)a/255.0];
}

- (UIColor *)darkThanCurrent:(CGFloat)value {
    
    CGFloat r = 0;
    CGFloat g = 0;
    CGFloat b = 0;
    CGFloat a = 1;
    
    [self getRed:&r green:&g blue:&b alpha:&a];
    
    r = r * r * value;
    g = g * g * value;
    b = b * b * value;
    
    return [UIColor colorWithRed:r green:g blue:b alpha:a];
}

- (UIColor *)lightThanCurrent:(CGFloat)value {
    
    CGFloat r = 0;
    CGFloat g = 0;
    CGFloat b = 0;
    CGFloat a = 1;
    
    [self getRed:&r green:&g blue:&b alpha:&a];
    
    r = 1 - r;
    g = 1 - g;
    b = 1 - b;
    
    r = r * r * value;
    g = g * g * value;
    b = b * b * value;
    
    return [UIColor colorWithRed:1 - r green:1 - g blue:1 - b alpha:a];
}

@end
